package com.base.sharding.transaction.config;

import com.base.sharding.transaction.aspect.ShardingTransactionAspect;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @Author liuyjy
 * @Date 2022/1/25 19:22
 * @Description: https://blog.csdn.net/qq_42815754/article/details/120475227
 * <p>
 * https://shardingsphere.apache.org/document/legacy/3.x/document/cn/manual/sharding-jdbc/usage/transaction/
 **/
@Configuration
@EnableTransactionManagement
public class TransactionConfiguration {

    @ShardingTransactionType(TransactionType.XA)
    @Bean
    public PlatformTransactionManager txManager(final DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public ShardingTransactionAspect getShardingTransactionAspect() {
        return new ShardingTransactionAspect();
    }

    @Bean
    public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
